<?php

namespace App\BO\ConvertibleCard;

use App\Entities\ConvertibleCardEntity;
use App\Entities\ConvertibleCardModel;
use App\Entities\ConvertibleCardUseRecordModel;
use App\Entities\UserConvertibleCardEntity;
use App\Entities\UserConvertibleCardModel;

class Helper
{
    public static function calculateStatus(ConvertibleCardEntity $cardEntity)
    {
        $curTime = time();
        $saleStart = strtotime($cardEntity->getStartSale());
        $saleEnd = strtotime($cardEntity->getEndSale());
        $onSale = $cardEntity->getOnSale();
        if ($curTime > $saleEnd) {
            return 4; #已过期
        } elseif (!$onSale) {
            return 3; #已下架
        } elseif ($curTime > $saleStart) {
            return 2; #销售中
        } elseif ($curTime < $saleStart) {
            return 1; #未开售
        }
    }

    //元 -> 分
    public static function yuan2fen(int $price)
    {
        return $price * 100;
    }

    //分 -> 元
    public static function fen2yuan(int $price)
    {
        return $price ? $price / 100 : 0;
    }

    public static function getConvertibleCardEntityList()
    {
        $entityList = [];
        $where = [];
        $data = ConvertibleCardModel::where($where)->get();
        foreach ($data as $cardModel) {
            $entityList[] = new ConvertibleCardEntity($cardModel);
        }
        return $entityList;
    }

    public static function getUserAvailableCardCount(ConvertibleCardEntity $cardEntity, int $userId)
    {
        $time = time();
        $cardId = $cardEntity->getId();
        $where = [
            'user_id' => $userId,
            'convertible_card_id' => $cardId,
            ['expire_date', '>', date('Y-m-d', $time)],
        ];
        //未过期的卡
        $nonExpiredUserCards = UserConvertibleCardModel::where($where)->get();
        $availableCards = [];
        if ($nonExpiredUserCards) {
            foreach ($nonExpiredUserCards as $nonExpiredUserCardModel) {
                $userCardEntity = new UserConvertibleCardEntity($nonExpiredUserCardModel);
                if(!self::isCardUsedUp($userCardEntity)) {
                    $availableCards[] = $userCardEntity->getId();
                }
            }
        }
        return count(array_unique(array_filter($availableCards)));
    }

    //卡片已被用光判断
    public static function isCardUsedUp(UserConvertibleCardEntity $userCardEntity)
    {
        $remain = self::userCardRemain($userCardEntity);
        if ($remain > 0) {
            return false;
        }
        return true;
    }

    public static function userCardRemain(UserConvertibleCardEntity $userCardEntity)
    {
        //购卡记录
        $where = [
            'user_card_id' => $userCardEntity->getId(),
            'type' => 1,
        ];
        $purchaseCount = ConvertibleCardUseRecordModel::where($where)->sum('value');

        //消费记录
        $where = [
            'user_card_id' => $userCardEntity->getId(),
            'type' => 2,
        ];
        $useCount = ConvertibleCardUseRecordModel::where($where)->sum('value');

        //恢复记录
        $where = [
            'user_card_id' => $userCardEntity->getId(),
            'type' => 3,
        ];
        $recoverCount = ConvertibleCardUseRecordModel::where($where)->sum('value');

        $remain = $purchaseCount + $recoverCount - $useCount;
        $remain = max(0, $remain);

        return $remain;
    }
}
